package com.wcm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.thymeleaf.spring4.view.ThymeleafView;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class WebConfigurerAdapter extends WebMvcConfigurerAdapter {
    @Autowired
    MultiViewResolver  resolver;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    }
    @Bean(name = "viewResolver")
    public MultiViewResolver getViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setViewClass(JstlView.class);
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setViewClass(ThymeleafView.class);
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
        Map<String, ViewResolver> resolvers = new HashMap<>();
        resolvers.put("jsp",internalResourceViewResolver);
        resolvers.put("th",thymeleafViewResolver);
        resolvers.put("ftl",freeMarkerViewResolver);

        resolver.setResolvers(resolvers);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
